package com.glacier.nacos.cmdb.plugin;

import com.alibaba.nacos.api.cmdb.pojo.Entity;
import com.alibaba.nacos.api.cmdb.pojo.EntityEvent;
import com.alibaba.nacos.api.cmdb.pojo.Label;
import com.alibaba.nacos.api.cmdb.pojo.PreservedEntityTypes;
import com.alibaba.nacos.api.cmdb.spi.CmdbService;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * date 2023-12-06 10:42
 *
 * @author glacier
 * @version 1.0
 */
public class CmdbServiceImpl implements CmdbService {

    /**
     * 实体
     * 类型： ( 实体名称 : 实体)
     */
    private final Map<String, Map<String, Entity>> entityMap = new ConcurrentHashMap<>();

    /**
     * 标签
     * 标签名称 : 标签值
     */
    private final Map<String, Label> labelMap = new ConcurrentHashMap<>();

    /**
     * 初始化
     */
    public CmdbServiceImpl() {
        Label label = new Label();
        label.setName("cluster");
        Set<String> values = new HashSet<>();
        values.add("shenzhen");
        values.add("shanghai");
        label.setValues(values);
        labelMap.put(label.getName(), label);
        entityMap.put(PreservedEntityTypes.ip.name(), new HashMap<>());

        //深圳cluster的ip
        Entity entityShen = new Entity();
        entityShen.setName("192.168.10.1");
        entityShen.setType(PreservedEntityTypes.ip.name());
        Map<String, String> lableShen = new HashMap<>();
        lableShen.put("cluster", "shenzhen");
        entityShen.setLabels(lableShen);
        entityMap.get(PreservedEntityTypes.ip.name()).put(entityShen.getName(), entityShen);

        //上海cluster的ip
        Entity entityShang = new Entity();
        entityShang.setName("192.168.20.1");
        entityShang.setType(PreservedEntityTypes.ip.name());
        Map<String, String> lableShang = new HashMap<>();
        lableShang.put("cluster", "shanghai");
        entityShang.setLabels(lableShang);
        entityMap.get(PreservedEntityTypes.ip.name()).put(entityShang.getName(), entityShang);
    }

    @Override
    public Set<String> getLabelNames() {
        return new HashSet<>(labelMap.keySet());
    }

    @Override
    public Set<String> getEntityTypes() {
        return new HashSet<>(entityMap.keySet());
    }

    @Override
    public Label getLabel(String labelName) {
        return labelMap.get(labelName);
    }

    @Override
    public String getLabelValue(String entityName, String entityType, String labelName) {
        return getLabelValues(entityName, entityType)
                .get(labelName);
    }

    @Override
    public Map<String, String> getLabelValues(String entityName, String entityType) {
        return getEntity(entityName, entityType)
                .getLabels();
    }

    @Override
    public Map<String, Map<String, Entity>> getAllEntities() {
        return entityMap;
    }

    @Override
    public List<EntityEvent> getEntityEvents(long timestamp) {
        // todo 暂不实现
        return null;
    }

    @Override
    public Entity getEntity(String entityName, String entityType) {
        return entityMap.get(entityType)
                .get(entityName);
    }
}
